package com.party.controller;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.party.domain.Topic;
import com.party.domain.User;
import com.party.dto.Page;
import com.party.dto.Response;
import com.party.service.TopicService;
/**
 * 
 * @author Caizhf
 * @date 2017年5月13日下午12:37:03
 * @version v.0.1
 * @email 1115054416@qq.com 
 *
 * <p>Description: Topic控制层</p>
 *
 */
//目前还没有做登录判断，打算使用AOP统一拦截查看用户是否登录
@RestController
@RequestMapping("/topic")
public class TopicController {
	@Autowired
	private TopicService topicService;
	private static final Logger logger = LoggerFactory.getLogger(TopicController.class);
	
	
	/**
	 * topic中要传入user的id：user.id
	 * @param t
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/add")
	public Response addTopic(Topic t){
		//从session取出user
		User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		t.setUser(user);
		Topic topic = topicService.add(t);
		return Response.success(topic);
	}
	
	
	@RequestMapping(value="/delete/{id}")
	public Response deleteTopicById(@PathVariable int id){
		topicService.deleteOne(id);
		return Response.success("删除成功");
	}
	
	@RequestMapping("/update")
	public Response updateTopic(Topic t){
		topicService.updateOne(t);
		return Response.success("更新成功");
	}
	
	
	/**
	 * 
	 * @param user_id 用户id
	 * @param pageNum 浏览页码，默认每页10条数据
	 * @return
	 * @throws Exception 
	 */
	/*@RequestMapping("/all/{user_id}/{pageNum}")
	public Response getUserAllTopicByUserId(@PathVariable int user_id,@PathVariable int pageNum){
		return Response.success(topicService.getUserAllTopic(user_id, pageNum));
	}*/
	@RequestMapping("/userall/{pageNum}")
	public Response getUserAllTopicByUserId(@PathVariable int pageNum){
		User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		return Response.success(topicService.getUserAllTopic(user.getId(), pageNum));
	}
	
	
	@RequestMapping(value="/get/{id}")
	public Response getTopicById(@PathVariable int id){
		return Response.success(topicService.getOne(id));
	}
	
	/**
	 * 
	 * @param user_id 点赞的用户
	 * @param topic_id 被点赞的topic
	 * @return
	 * @throws Exception 
	 */
	/*@RequestMapping("/like/{user_id}/{topic_id}")
	public Response likeThisTopicClick(@PathVariable String user_id,@PathVariable int topic_id){
		topicService.likeClick(user_id,topic_id);
		return Response.success();
	}*/
	@RequestMapping("/like/click/{topic_id}")
	public Response likeThisTopicClick(@PathVariable int topic_id){
		User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		topicService.likeClick(user.getId()+"",topic_id);
		return Response.success();
	}
	
	/**
	 * 点赞人数
	 * @param topic_id
	 * @return
	 */
	@RequestMapping("/like/{topic_id}")
	public Response likeThisTopicNum(@PathVariable int topic_id){
		int record = topicService.likeRecord(topic_id);
		return Response.success(record);
	}
	
	@RequestMapping("/list/{pageNum}")
	public Response list(@PathVariable("pageNum") int pageNum){
		Page p = topicService.getAllTopic(pageNum);
		return Response.success(p);
	}
}
